package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.Map;

import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

public class FeedBackActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private LinearLayout topRightLayout;
	private EditText contentEt;

	/****************** 数据变量 **************************/
	private String contentStr;
	private Map<String, Object> resultMap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.topRightLayout = (LinearLayout) findViewById(R.id.ll_common_right);
		this.contentEt = (EditText) findViewById(R.id.feedback_content_et);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_common_right:
					contentStr = contentEt.getText().toString().trim();
					requestAddFeedBackData();
					break;

				default:
					break;
				}
			}
		};
		this.topRightLayout.setOnClickListener(moreClick);
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	// TODO 获取发布活动数据
	/**
	 * 获取发布活动数据
	 */
	private void requestAddFeedBackData() {
		AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> addDishParamMap = new HashMap<String, Object>();
						addDishParamMap.put("userID", commonUserInfo.getUserId());
						addDishParamMap.put("content", contentStr);
						// 发送请求
						byte[] bRecommend = HttpUtil.requestByPost(
								FeedBackActivity.this,
								Constants.ADD_FEEDBACK_API, addDishParamMap);
						if (bRecommend != null) {
							resultMap = new ParseJson()
									.parseAddResultJson(bRecommend);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						boolean returnFlg = (Boolean) resultMap
								.get("returnFlg");
						if (returnFlg) {
							showShortToast(FeedBackActivity.this, resultMap
									.get("regMessage").toString(), true);
							finish();
						} else {
							System.out.println(resultMap.get("regMessage")
									.toString());
							showShortToast(FeedBackActivity.this, "添加失败",
									true);
						}
					}

				});
		ald.execute(0);

	}
}